import { _decorator, CCString, Component, Label, Node, RichText, tween, EventTarget, Event, Touch, Input, UIOpacity } from 'cc';
import { DialogEvent, EventType, ROLE_TYPE, STATE } from 'db://assets/enum';
import { Logger } from 'db://assets/util';
import { ServiceLocator } from '../../service';
import { Printer } from '../../components/Printer';
const { ccclass, property, menu } = _decorator;

@ccclass('MinorContainer')
@menu('container/Minor')
export class MinorContainer extends Component {
    #logger = new Logger("次对话容器::MinorContainer");

    @property(Label)
    nickNameNode: Label

    @property(CCString)
    nickName: string;

    @property(CCString)
    context: string;

    get printer() {
        return this.getComponent(Printer)
    }

    get UIOpacity() {
        return this.getComponent(UIOpacity)
    }

    onEnable() {
        this.#logger.debug("#onEnable");
        this.nickNameNode.string = this.nickName
        this.printer.printerStr = this.context
        this.fadeIn(() => {
            this.getComponent(Printer).printAnime()
        })
    }

    protected start(): void {
        this.#logger.debug("#start");
    }

    protected onLoad(): void {
        this.#logger.debug("#onLoad");
    }

    protected onDisable(): void {
        this.#logger.debug("#onDisable");
        this.nickNameNode.string = ''
    }

    fadeIn(cb = () => { }) {
        tween(this.UIOpacity)
            .to(0.2, { opacity: 255 }, { easing: 'fade' })
            .call(cb)
            .start()
    }

    fadeOut(cb = () => { }) {
        tween(this.UIOpacity)
            .to(0.2, { opacity: 0 }, { easing: 'fade' })
            .call(cb)
            .start()
    }

    clear(): Promise<void> {
        return new Promise(resolve => {
            this.fadeOut(() => {
                this.node.active = false
                resolve()
            })
        })
    }
}
